<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="@{/}">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
</head>
<body>
<!--管理员：统计管理交互界面-->
<div class="easyui-tabs" style="width:100%;height:100%">
    <div title="已完成统计的信息列表" style="padding:2px">
        <table id="dg_statisticsFinisheds"  style="width:100%;height:100%">

        </table>
        <div id="statisticsFinishedsToolbar">
            <div class="param-div">
            <select id="ex_statisticsTableName" class="form-control-sm" name="ex_statisticsTableName" >
                <option value="">请选择统计表...</option>
                <option th:each="statis:${session.statistics}" th:value="${statis.getStatisticsTableName()}" th:text="${statis.getStatisticsName()}"></option>
            </select>
            </div>
            <div class="param-div">
                <select id="ex_gradeDatatableName" class="form-control-sm" name="ex_gradeDatatableName">
                    <option value="">请选择年级...</option>
                    <option th:each="gra:${session.grades}" th:value="${gra.getGradeDatatableName()}" th:text="${gra.getGradeName()}"></option>
                </select>
            </div>
            <div class="param-div">
                <select id="ex_semesterName" class="form-control-sm" name="ex_semesterName">
                    <option value="">请选择学期...</option>
                    <option th:each="semes:${session.semesters}" th:value="${semes.getSemesterName()}" th:text="${semes.getSemesterName()}"></option>
                </select>
            </div>
            <div class="param-div" style="width: 280px;">
                <select id="ex_examName" class="form-control-sm" name="ex_examName" style="width: 280px;">
                    <option value="">请选择考试...</option>
                    <option th:each="exa:${session.exams}" th:value="${exa.getExamName()}" th:text="${exa.getExamName()}"></option>
                </select>
            </div>
            <div class="param-div">
                <select id="ex_categoryName" class="form-control-sm" name="ex_categoryName">
                    <option value="">请选择统分类...</option>
                    <option th:each="cate:${session.categorys}" th:value="${cate.getCategoryName()}" th:text="${cate.getCategoryName()}"></option>
                </select>
            </div>
            <div class="param-button">
                <a href="javascript:void(0)" class="easyui-linkbutton"   id="searchBtn"  iconCls="icon-search" onclick="doSearchStatisticsFinishedData()">搜索</a>
            </div>
            <div class="param-button">
                <a href="javascript:void(0)" class="easyui-linkbutton"  id="statisticBtn" iconCls="icon-add" onclick="doScoreStatistics()">添加</a>
            </div>
            <div class="param-button">
                <a href="javascript:void(0)" class="easyui-linkbutton"   id="deleteBtn" iconCls="icon-remove" onclick="doDeleteStatisticsFinishedData()">删除</a>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" th:inline="none">
    $(function () {
        //分页参数
        let pageParam = {
            pageSize: 30,							//每页显示的记录条数，默认为10
            pageList: [10, 20, 30, 50, 100],	//可以设置每页记录条数的列表
            beforePageText: '第',				//页数文本框前显示的汉字
            afterPageText: '页    共 {pages} 页',
            displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        }
        //已完成统计信息显示(默认全部显示)
        $("#dg_statisticsFinisheds").datagrid({
            url: "searchStatisticsFinishedData",
            queryParams:{
                semesterName: $("#ex_semesterName").val(),
                gradeDatatableName:$("#ex_gradeDatatableName").val(),
                examName: $("#ex_examName").val(),
                categoryName: $("#ex_categoryName").val()
            },
            toolbar: "#statisticsFinishedsToolbar",
            pagination: true,
            pageSize: 30,
            rownumbers: true,
            fitColumns: true,
            singleSelect: false, // 单行:true ,多行：false
            fit: true,
            checkOnSelect:true,
            columns: [[
                {field: 'chk', checkbox: true, width: 50},
                {field: 'statisticsFinishedId', title: '编号', align:'center', hidden:true, width: 50,sortable: true},
                {field: 'statisticsName', title: '统计表名称', align:'center', width: 150, sortable: true},
                {field: 'gradeName', title: '年级名称', align:'center',width: 150},
                {field: 'semesterName', title: '学期',align:'center', width: 150},
                {field: 'examName', title: '考试名称', align:'center',width: 150},
                {field: 'categoryName', title: '分类',align:'center', width: 100},
                {field: 'statisticsDate', title: '统计时间', align:'center',width: 100}
            ]]
        });
        let pageStatisticsFinisheds = $("#dg_statisticsFinisheds").datagrid("getPager");
        $(pageStatisticsFinisheds).pagination(pageParam);
    });
    // 添加：根据选择的（统计报表，学期，年级，考试，分类）进行统计
    function doScoreStatistics(){
        // 判断选择的有效性：
        let statisticsTableName = $("#ex_statisticsTableName").val();
        let gradeDatatableName= $("#ex_gradeDatatableName").val();
        let semesterName = $("#ex_semesterName").val();
        let examName = $("#ex_examName").val();
        let categoryName = $("#ex_categoryName").val();
        if (statisticsTableName.length<=0){
            $.messager.alert("提示","必须“选择统计表”,明确统计的项目！","info")
        } else if (gradeDatatableName.length<=0){
            $.messager.alert("提示","必须“选择年级”，明确数据来源！","info")
        } else if (semesterName.length<=0){
            $.messager.alert("提示","必须“选择学期”,明确数据来源！","info")
        } else if (examName.length<=0){
            $.messager.alert("提示","必须“选择考试”，明确数据来源！","info")
        } else if (categoryName.length<=0){
            $.messager.alert("提示","必须“选择分类”，明确数据来源！","info")
        } else {
            $.ajax({
                type: "post",
                url: "scoreStatistics",
                data: {"statisticsTableName":statisticsTableName,
                    "semesterName":semesterName,
                    "gradeDatatableName":gradeDatatableName,
                    "examName":examName,
                    "categoryName":categoryName},
                dataType:'json',
                async:true,
                success: function(res){
                    //强制刷新表格
                    $("#dg_statisticsFinisheds").datagrid(
                        {
                            url: "searchStatisticsFinishedData",
                            queryParams:{
                                semesterName: $("#ex_semesterName").val(),
                                gradeDatatableName:$("#ex_gradeDatatableName").val(),
                                examName: $("#ex_examName").val(),
                                categoryName: $("#ex_categoryName").val()
                            },
                            method:"post"
                        }
                    );
                    $("#dg_statisticsFinisheds").datagrid("uncheckAll");
                    // 提示：
                    if(res.type == "success"){
                        $.messager.alert("消息提醒",res.msg,"info");
                    } else{
                        $.messager.alert("消息提醒",res.msg,"warning");
                    }
                }
            });
        }
    }
    // 查询：只显示当前选中的（学期，年级，考试，分类）的统计信息
    function doSearchStatisticsFinishedData(){
        // 判断选择的有效性：测试成功
        let gradeDatatableName= $("#ex_gradeDatatableName").val();
        let semesterName = $("#ex_semesterName").val();
        let examName = $("#ex_examName").val();
        let categoryName = $("#ex_categoryName").val();

        if (gradeDatatableName.length<=0){
            $.messager.alert("提示","必须“选择年级”，明确查询条件！","info")
        } else if (semesterName.length<=0){
            $.messager.alert("提示","必须“选择学期”,明确查询条件！","info")
        } else if (examName.length<=0){
            $.messager.alert("提示","必须“选择考试”，明确查询条件！","info")
        } else if (categoryName.length<=0){
            $.messager.alert("提示","必须“选择分类”，明确查询条件！","info")
        } else {
            $("#dg_statisticsFinisheds").datagrid("load", {
                "semesterName": $("#ex_semesterName").val(),
                "gradeDatatableName": $("#ex_gradeDatatableName").val(),
                "examName": $("#ex_examName").val(),
                "categoryName": $("#ex_categoryName").val()
            });
        }
    }

    // 每次选择一条记录删除：“已统计完成记录并且删除对应统计表中的统计记录”
    function doDeleteStatisticsFinishedData(){
        let selectRows = $("#dg_statisticsFinisheds").datagrid("getSelections");
        let selectLength = selectRows.length;
        if(selectLength == 0){
            $.messager.alert("消息提醒", "请从“已完成统计的信息列表”中选择“1条”统计记录进行删除!", "warning");
        } else if (selectLength>1) {
            $.messager.alert("消息提醒", "只能选择“1条”统计记录进行删除，请重新选择!", "warning");
        } else {
            let statisticsTableName="";
            let semesterName="";
            let gradeName="";
            let examName="";
            let categoryName="";
            $(selectRows).each(function(i, row){
                statisticsTableName=row.statisticsTableName;
                semesterName=row.semesterName;
                gradeName=row.gradeName;
                examName=row.examName;
                categoryName=row.categoryName;
            });

            $.messager.confirm("消息提醒", "确定删除该条统计数据？", function(r){
                if(r){
                    $.ajax({
                        type: "post",
                        url: "deleteStatisticsFinishedData",
                        data: {"statisticsTableName":statisticsTableName,
                        "semesterName":semesterName,
                        "gradeName":gradeName,
                        "examName":examName,
                        "categoryName":categoryName},
                        dataType:'json',
                        async:true,
                        success: function(res){
                            if(res.type == "success"){
                                $.messager.alert("消息提醒",res.msg,"info");
                                //刷新表格
                                $("#dg_statisticsFinisheds").datagrid("reload");
                                $("#dg_statisticsFinisheds").datagrid("uncheckAll");
                            } else{
                                $.messager.alert("消息提醒",res.msg,"warning");
                            }
                        }
                    });
                }
            });
        }
    }
</script>
</body>